/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package managedbean;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.Resource;
import javax.inject.Named;
import javax.enterprise.context.Dependent;
import javax.sql.DataSource;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;

/**
 *
 * @author Andy
 */
@Named(value = "reportsManagerBean")
@Dependent
public class ReportsManagerBean {

    @Resource(name = "macteamDataSource")
    private DataSource macteamDataSource; 
    
    private static final int DEFAULT_BUFFER_SIZE = 10240; // 10KB.
    
    public void exportReportToPdf() throws JRException, FileNotFoundException, IOException{
        
        Connection conn = null;
        JasperReport jasperreport;
        JasperPrint jasperprint;

        try {
            conn = macteamDataSource.getConnection();
        } catch (SQLException ex) {
            Logger.getLogger(ReportsManagerBean.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        HashMap jasperParameter = new HashMap();
        
        jasperreport = JasperCompileManager.compileReport(System.getProperty("user.home")+"/NetBeansProjects/MacTeam/MacTeam-war/web/jasperreports/testReport.jrxml");
        
        jasperprint = JasperFillManager.fillReport(jasperreport, jasperParameter, conn);

        JasperExportManager.exportReportToPdfFile(jasperprint, System.getProperty("user.home")+"/NetBeansProjects/MacTeam/MacTeam-war/web/jasperreports/testReport.pdf");
        JasperExportManager.exportReportToHtmlFile(jasperprint, System.getProperty("user.home")+"/NetBeansProjects/MacTeam/MacTeam-war/web/jasperreports/testReport.html");
        
        //return "testReport.pdf";
//        FacesContext facesContext = FacesContext.getCurrentInstance();
//        ExternalContext externalContext = facesContext.getExternalContext();
//        HttpServletResponse response = (HttpServletResponse) externalContext.getResponse();
//        
//        File file = new File(System.getProperty("user.home")+"/NetBeansProjects/MacTeam/MacTeam-war/web/resources/", "testReport.html");
//        BufferedInputStream input = null;
//        BufferedOutputStream output = null;
//        
//        try {
//            // Open file.
//            input = new BufferedInputStream(new FileInputStream(file), DEFAULT_BUFFER_SIZE);
//
//            // Init servlet response.
//            response.reset();
//            response.setHeader("Content-Type", "application/pdf");
//            response.setHeader("Content-Length", String.valueOf(file.length()));
//            response.setHeader("Content-Disposition", "inline; filename=\"" + "testReport.html" + "\"");
//            output = new BufferedOutputStream(response.getOutputStream(), DEFAULT_BUFFER_SIZE);
//
//            // Write file contents to response.
//            byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
//            int length;
//            while ((length = input.read(buffer)) > 0) {
//                output.write(buffer, 0, length);
//            }
//
//            // Finalize task.
//            output.flush();
//        } finally {
//            // Gently close streams.
//            close(output);
//            close(input);
//        }
//
//        // Inform JSF that it doesn't need to handle response.
//        // This is very important, otherwise you will get the following exception in the logs:
//        // java.lang.IllegalStateException: Cannot forward after response has been committed.
//        facesContext.responseComplete();
//    }
//        
//        private static void close(Closeable resource) {
//        if (resource != null) {
//            try {
//                resource.close();
//            } catch (IOException e) {
//                // Do your thing with the exception. Print it, log it or mail it. It may be useful to 
//                // know that this will generally only be thrown when the client aborted the download.
//                e.printStackTrace();
//            }
//        }
    }

    /** Creates a new instance of ReportsManagerBean */
    public ReportsManagerBean() {
    }
}
